package day03;

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        //查找整数
        //用二分法查找
        int[] nums = {15,25,36,66,42,8,24,96,18,23};
        int t;
        //先排序,冒泡排序
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = 0; j < nums.length - i - 1; j++) {
                if(nums[j] > nums[j+1]){
                    t = nums[j];
                    nums[j] = nums[j+1];
                    nums[j+1] = t;
                }
            }
        }
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i] + " ");
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您需要找的值：");
        int b = sc.nextInt();
        int min = 0;//最小下标
        int max = nums.length - 1;//大下标
        int middle = (min + max) / 2;//中间下标
//        for (int i = 0; i < nums.length; i++) {
            //System.out.println(nums[i]);//遍历输出打印好的值
        while (true) {
            if (b > nums[middle]) {
                min = middle + 1;
            } else if (b < nums[middle]) {
                max = middle - 1;
            } else {
                break;
            }
            if (min > max) {
                System.out.println("找不到该数字");
                break;
            }
            middle = (min +max)/2;
        }
        System.out.println("下标是：" + middle);
//        }
        //二分法查找

    }
}
